package cn.uppower.wordaliy.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.LitePal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.activity.FavoriteWordsActivity;
import cn.uppower.wordaliy.activity.LoginActivity;
import cn.uppower.wordaliy.activity.SettingActivity;
import cn.uppower.wordaliy.model.FavoriteWords;
import cn.uppower.wordaliy.model.Words;

/**
 * Created by elitetyc on 2018/11/26.
 * Describe:
 */
public class AboutMeFragment extends Fragment {


    private TextView tv_userPhone,tv_registeTime;
    private LinearLayout ll_my_favorite_words;
    private Context context;
    private ImageView iv_mysetting;

    private RelativeLayout rl_logout,rl_about_us,rl_problem,rl_suggest,rl_version,rl_share;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getActivity(),R.layout.fragment_aboutme,null);
        context=getActivity();
        initView(view);


        tv_userPhone.setText("用户:"+WordaliyApplication.userInfo.getUserPhone());
        tv_registeTime.setText("注册时间:"+WordaliyApplication.userInfo.getRegisteTime());

        ll_my_favorite_words.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<FavoriteWords> favoriteWords=LitePal.where("userPhone = ?",WordaliyApplication.userInfo.getUserPhone()).find(FavoriteWords.class);
                List<Words> wordsList =new ArrayList<>();
                for (FavoriteWords favoriteWords1:favoriteWords){
                    wordsList.add(LitePal.where("id = ?",String.valueOf(favoriteWords1.getWordsId())).findFirst(Words.class));
                }
                Intent intent =new Intent(context,FavoriteWordsActivity.class);
                intent.putExtra("favWords", (Serializable) wordsList);
                startActivity(intent);
            }
        });
        rl_about_us.setOnClickListener((e)-> Toast.makeText(context, "wordaliy开发团队。。", Toast.LENGTH_SHORT).show());
        rl_problem.setOnClickListener((e)-> Toast.makeText(context, "常见问题。。", Toast.LENGTH_SHORT).show());
        rl_suggest.setOnClickListener((e)-> Toast.makeText(context, "意见反馈。。", Toast.LENGTH_SHORT).show());
        rl_version.setOnClickListener((e)-> Toast.makeText(context, "当前以是最新版本，无需升级", Toast.LENGTH_SHORT).show());
        rl_share.setOnClickListener((e)-> Toast.makeText(context, "分享给好友", Toast.LENGTH_SHORT).show());


        rl_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder normalDialog = new AlertDialog.Builder(context);
                normalDialog.setIcon(R.mipmap.wring);
                normalDialog.setTitle("退出登录");
                normalDialog.setMessage("你要确认要退出登录吗？");
                normalDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                             //                删除用户登录信息
                                WordaliyApplication.wordaliyEditor.clear();
                                WordaliyApplication.wordaliyEditor.commit();
                                WordaliyApplication.userInfo.setUserPhone(null);
                //                进入登录界面
                                startActivity(new Intent(context,LoginActivity.class));
                            }
                        });
                normalDialog.setNegativeButton("关闭",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                // 显示
                normalDialog.show();
            }
        });

        iv_mysetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context,SettingActivity.class));
            }
        });

        return view;
    }

    private void initView(View view) {
        tv_userPhone = view.findViewById(R.id.tv_userPhone);
        tv_registeTime = view.findViewById(R.id.tv_registeTime);
        ll_my_favorite_words = view.findViewById(R.id.ll_my_favorite_words);
        rl_logout = view.findViewById(R.id.rl_logout);
        rl_about_us = view.findViewById(R.id.rl_about_us);
        rl_problem = view.findViewById(R.id.rl_problem);
        rl_suggest = view.findViewById(R.id.rl_suggest);
        rl_version = view.findViewById(R.id.rl_version);
        rl_share = view.findViewById(R.id.rl_share);
        iv_mysetting = view.findViewById(R.id.iv_mysetting);
    }
}
